import { defineStore } from "pinia";
import { ElMessage } from "element-plus";
import { ref, reactive } from "vue"
import { useEnumStore } from "@/store/enumeration"
import {equityPagePost,equityDeleteDelete,equityDetailGet,equityListDataByIdGet} from "@/api/equity"
import useInstance from '@/hooks/useInstance';
export const equityListStore = defineStore("equityListStoreId", () => {
const { global } = useInstance();
const detailRef = ref<{ show: (title: string, row?: any) => void }>()
const quotaRef = ref<{ show: (type: string, row?: any) => void }>()
const inventoryRef = ref<{ show: (type: string, row?: any) => void }>()


    const { expiryPeriodType, reductionVoucherType,isShow ,quotaOperateType} = useEnumStore()
    const detailData = ref([
        {
            prop: "supplierCode",
            name: "供应商编码",
            type: "text",
            value: null
        },
        {
            prop: "supplierName",
            name: "供应商名称",
            type: "text",
            value: null
        },
        {
            prop: "supplierReductionVoucherCode",
            name: "供应商立减金编码",
            type: "text",
            value: null
        },
        {
            prop: "reductionVoucherType",
            name: "立减金类型",
            type: "text",
            value: null
        },
        {
            prop: "reductionVoucherCode",
            name: "立减金编码",
            type: "text",
            value: null
        },
        {
            prop: "reductionVoucherName",
            name: "立减金名称",
            type: "text",
            value: null
        },
        {
            prop: "reductionVoucherAmount",
            name: "立减金额度",
            type: "text",
            value: null
        },
        {
            prop: "expiryPeriod",
            name: "过期期限",
            type: "text",
            value: null
        },
        {
            prop: "expiryPeriodType",
            name: "过期期限类型",
            type: "text",
            value: null
        },
        {
            prop: "remainderQuota",
            name: "剩余配额",
            type: "text",
            value: null
        },
        {
            prop: "issuedQuota",
            name: "已发放配额",
            type: "text",
            value: null
        },
        {
            prop: "receivedQuota",
            name: "已领取配额",
            type: "text",
            value: null
        },
        {
            prop: "usedQuota",
            name: "已使用配额",
            type: "text",
            value: null
        },
        {
            prop: "enabled",
            name: "是否启用",
            type: "text",
            value: null
        },
        {
            prop: "extInfo",
            name: "扩展信息",
            type: "text",
            value: null
        },
    ])
    const tableProp = reactive(
        {
            name: "supplierList",
            propsList: [

                {
                    prop: 'reductionVoucherName',
                    label: '立减金名称',
                    type: 'normal',
                    minWidth: '150px',
                    hidden: true,
                },
                {
                    prop: 'reductionVoucherType',
                    label: '立减金类型',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'reductionVoucherCode',
                    label: '立减金编码',
                    type: 'normal',
                    minWidth: '170px',
                    hidden: true,
                },
      
                {
                    prop: 'reductionVoucherAmount',
                    label: '立减金额度',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'supplierCode',
                    label: '供应商编码',
                    type: 'normal',
                    minWidth: '150px',
                    hidden: true,
                },
                {
                    prop: 'supplierName',
                    label: '供应商名称',
                    type: 'normal',
                    minWidth: '170px',
                    hidden: true,
                },
                {
                    prop: 'supplierReductionVoucherCode',
                    label: '供应商立减金编码',
                    type: 'normal',
                    minWidth: '130px',
                    hidden: true,
                },
                {
                    prop: 'issuedQuota',
                    label: ' 已发放配额',
                    type: 'normal',
                    minWidth: '90px',
                    hidden: true,
                },
                {
                    prop: 'receivedQuota',
                    label: ' 已领取配额',
                    type: 'normal',
                    minWidth: '90px',
                    hidden: true,
                },
                {
                    prop: 'usedQuota',
                    label: ' 已使用配额',
                    type: 'normal',
                    minWidth: '90px',
                    hidden: true,
                },
                {
                    prop: 'remainderQuota',
                    label: '剩余配额',
                    type: 'normal',
                    minWidth: '80px',
                    hidden: true,
                },
                {
                    prop: 'expiryPeriod',
                    label: '过期期限',
                    type: 'normal',
                    minWidth: '70px',
                    hidden: true,
                },
                {
                    prop: 'expiryPeriodType',
                    label: '过期期限类型',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'enabled',
                    label: ' 是否启用', //是-Y，否-N
                    type: 'normal',
                    minWidth: '70px',
                    hidden: true,
                },
                {
                    prop: 'extInfo',
                    label: ' 扩展信息',
                    type: 'normal',
                    minWidth: '150px',
                    hidden: true,
                },


                {
                    type: 'handle',
                    label: '操作',
                    minWidth: '250px',
                    btnType: ' link',
                    slotName: 'handle',
                    hidden: true,
                }
            ]
        })
    const listSearch = reactive({
        orders: ["created_time desc"],
        condition: {
            supplierCode: null, //供应商编码
            supplierReductionVoucherCode: null, //供应商立减金编码
            reductionVoucherTypeList: null, //立减金类型
            reductionVoucherCode: null, //立减金编码
            reductionVoucherName: null, //立减金名称
            reductionVoucherAmount: null, //立减金额度
        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: []

    })

    const getDate1 =async (type: string) => {
        pageParams.loadIng = true
        // if(listSearch.condition.reductionVoucherTypeList) {
        //     listSearch.condition.reductionVoucherTypeList =     Number(listSearch.condition.reductionVoucherTypeList)
        // }
        if (type == 'reset') {
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            Object.keys(listSearch.condition).forEach((it: any) => {
                    listSearch.condition[it] = null

            })
        }

        try {
            const res = await equityPagePost(listSearch)
            pageParams.loadIng = false
            if(res.code == 20000){
                pageParams.tableData = res.data.records && res.data.records.map((item:any)=>{
                    return{
                        ...item,
                        reductionVoucherType:reductionVoucherType[item.reductionVoucherType],
                        expiryPeriodType:expiryPeriodType[item.expiryPeriodType - 1]?.value,
                        enabled:isShow[item.enabled],
                        extInfo: item.extInfo == "{}" ? "" : item.extInfo
                    }
                })
                pageParams.total = res.data.total
            }
        } catch (error) {
            console.log(error);
            pageParams.loadIng = false
            
        }
    }
    const detailBtn = async (row: any) => {
        try {
            const res = await equityDetailGet({ id: row.id })
            res.data.reductionVoucherType = res.data.reductionVoucherType + ''
            res.data.expiryPeriodType = res.data.expiryPeriodType + ''         
            if (res.code != 20000) return
            detailData.value = detailData.value.map(item => {
 
                if (item.prop == "reductionVoucherType") {                        
                    return {
                        ...item,
                        value: reductionVoucherType[res.data[item.prop]]
                    }
                }else if(item.prop == "expiryPeriodType"){
                    return {
                        ...item,
                        value: expiryPeriodType[res.data[item.prop] - 1]?.value
                    }
                }else if(item.prop == "enabled"){
                    return {
                        ...item,
                        value: isShow[res.data[item.prop]]
                    }
                } else {
                    return {
                        ...item,
                        value: res.data[item.prop]
                    }
                }
            })
            detailRef.value.dialogBasic.height = 500
            detailRef.value?.show('edit')

        } catch (error) {

        }
    }
    const quotaBtn=async (row:any)=>{
        quotaRef.value?.show('add',row)
    }
    const handPage=(params:any)=>{
        listSearch.pageNum = params.pageNum
        listSearch.pageSize = params.pageSize
        getDate1('search')
    }
    const delBtn =async (row: any) => {
        // 
        if(row.enabled == '是'){
            return   ElMessage({
                type: "warning",
                message: "该数据启用状态 不能删除！"
            })
        }
        
        const flag = await global.$myconfirm(`是否确定删除该数据？  ${row.reductionVoucherName}`)
        if (flag) {
            const res = await equityDeleteDelete({ id: row.id })
            console.log(res.data);
            if (res.code == 20000) {
                ElMessage({
                    type: "success",
                    message: "数据删除成功"
                })
                getDate1('search')
            } else {
                ElMessage({
                    type: "error",
                    message: "数据删除失败"
                })
            }
        }
    }
    const lookInventory=async (row:any)=>{
        try {
            const res = await equityListDataByIdGet({id:row.id})
            res.data = res.data.map((item:any)=>{
                return {
                    ...item,
                    quotaOpType:quotaOperateType[item.quotaOpType]
                }
            })
            inventoryRef.value?.show('edit',res.data)
        } catch (error) {
            console.log(error);
        }
    }
    return {
        detailData, tableProp, listSearch, pageParams,detailRef,quotaRef,inventoryRef,
        getDate1,handPage,delBtn,detailBtn,quotaBtn,lookInventory
    }
})